package com.miplus.ccrm.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class IThread {
    public boolean isRunning = false;
    public boolean m_bExit=true;
    public String m_Name="";

    //getClass()可以打调用的父类ID
    private final Logger logger = LoggerFactory.getLogger(getClass());

    public IThread()
    {
    }

    public int Start(String name)
    {
        if(isRunning){
            logger.info("IThread({}).Start:HasRunning..",name);
            return 0;
        }
        logger.info("IThread({}).Start..",name);
        m_bExit=false;
        isRunning=false;
        m_Name=name;
        Thread h = new Thread(new Runnable() {
            @Override
            public void run() {
                isRunning = true;
                logger.info("IThread({}).Running..",m_Name);

                while (!m_bExit && isRunning) {
                    Thread.yield();
                    isRunning = OnRun();
                }

                isRunning=false;
                logger.info("IThread({}).Running Exit.",m_Name);
            }
        },name);

        h.start();

        return 0;
    }

    public void Stop()
    {
        logger.info("IThread({}).Stop..",m_Name);
        m_bExit=true;
    }

    protected boolean OnRun()
    {
        //此函数需要重载
        logger.info("IThread({}).OnRun..",m_Name);
        return false;
    }
}
